package com.mijie.homi.test;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.future.WriteFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.mijie.homi.common.constant.CategoryCode;
import com.mijie.homi.common.constant.MCKey;
import com.mijie.homi.common.constant.TypeCode;
import com.mijie.homi.common.protocol.HomiProtocol;
import com.mijie.homi.common.protocol.HomiProtocolCodecFactory;
import com.mijie.homi.service.cache.MemcacheService;
import com.mijie.homi.util.ServerHelper;
import com.mijie.util.Helper;

public class Client {

	public static void main(String[] args) throws InterruptedException{
		NioSocketConnector connector = new NioSocketConnector();  
		connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new HomiProtocolCodecFactory( Charset.forName( "UTF-8" ))));
		connector.setHandler(new ClientHandler());
		ConnectFuture cFuture = connector.connect(new InetSocketAddress("192.168.0.166",9997)); 
		cFuture.awaitUninterruptibly(1000*5);
		connector.getSessionConfig().setKeepAlive(true);
		login(cFuture);
	}
	
	public static void sendMessage(ConnectFuture future,int receiver,String message){
		IoSession session = future.getSession();
		HomiProtocol pack = new HomiProtocol(TypeCode.CHAT);
		JSONObject json = new JSONObject();
		json.accumulate("category", 1);
		json.accumulate("receiver", receiver);
		json.accumulate("cid", UUID.randomUUID().toString());
		json.accumulate("message", message);
		json.accumulate("image", "");
		json.accumulate("length", 100);
		pack.setContent(json.toString());
		System.out.println(json.toString());
		session.write(pack);
	}
	
	/*public static void serverCheckin(ConnectFuture future){
		HomiProtocol pack = new HomiProtocol(TypeCode.SERVER_CHECKIN);
		JSONObject json = new JSONObject();
		json.accumulate("serverId", "api");
		json.accumulate("serverPasswd", "520mijie4ever");
		pack.setContent(json.toString());
		System.out.println("serverCheckin:"+json);
		future.getSession().write(pack).awaitUninterruptibly();
	}*/
	
	public static void login(ConnectFuture future){
		HomiProtocol notify = new HomiProtocol(TypeCode.LOGIN);
		JSONObject json = new JSONObject();
		json.accumulate("nickName", "zhengzb@me.com");
		json.accumulate("passwd", Helper.base64encode(Helper.MD5("zhengzhibin")));
		json.accumulate("os", "ios");
		json.accumulate("appVer", "1.0.7");
		System.out.println("login"+json);
		notify.setContent(json.toString());
		future.getSession().write(notify).awaitUninterruptibly();
	}
	
	public static void sendMessage(ConnectFuture future,String msg){
		HomiProtocol notify = new HomiProtocol(TypeCode.CHAT);
		JSONObject json = new JSONObject();
		json.accumulate("category", "1");
		json.accumulate("receiver", "3");
		json.accumulate("counter", "1");
		json.accumulate("cid", UUID.randomUUID().toString());
		json.accumulate("message", msg);
		notify.setContent(json.toString());
		future.getSession().write(notify);
		System.out.println("send message->"+msg);
	}
}
